package com.chzc.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chzc.po.User;
import com.chzc.sys.req.UserREQ;
import com.chzc.sys.service.IUserService;
import com.chzc.util.Result;
import com.chzc.util.enums.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-24
 */
@Api(value = "用户管理接口(私有)", tags = "用户管理接口(私有)")
@RestController
@RequestMapping("/pri/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("根据token获取用户信息")
    @GetMapping("/getInfo")
    public User getUserInfo(){
        return userService.getUserInfo();
    }


    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public Result logout(){
        StpUtil.logout();
        return new Result(true, StatusCode.LOGOUT_SUCCESS.getCode(), StatusCode.LOGOUT_SUCCESS.getMessage());
    }

    @SaCheckPermission("user:search")
    @ApiOperation("根据用户名和手机号查询用户列表接口")
    @PostMapping("/search")
    public IPage<User> search(@RequestBody UserREQ req) {
        return userService.queryPage(req);
    }


    @ApiImplicitParam(name = "id", value = "用户Id", required = true)
    @ApiOperation("根据用户id查询所拥有的角色ids接口")
    @GetMapping("/{id}/role/ids")
    public List<String> findRoleIdsById(@PathVariable("id") String id) {
        return userService.findRoleIdsById(id);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户Id", required = true), @ApiImplicitParam(name = "roleIds", value = "角色Id集合", required = true, allowMultiple = true, dataType = "String"),})
    @ApiOperation("新增用户角色关系数据接口")
    @PostMapping("/{id}/role/save")
    public Result saveUserRole(@PathVariable("id") String id, @RequestBody List<String> roleIds) {
        return userService.saveUserRole(id, roleIds);
    }


    @SaCheckPermission("user:delete")
    @ApiOperation("通过用户ID删除用户接口")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        return userService.deleteById(id);
    }


    @SaCheckPermission("user:add")
    @ApiOperation("新增用户信息接口")
    @PostMapping // 请求地址 /user
    public Result save(@RequestBody User sysUser) {
        sysUser.setIsEnabled(1);
        sysUser.setCreateDate(new Date());
        // 新增
        userService.save(sysUser);
        return Result.addSuccess();
    }


    @ApiOperation("查询用户详情接口")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    @GetMapping("/{id}")
    public User view(@PathVariable("id") String id) {
        return userService.getById(id);
    }


    @ApiOperation("更新用户信息接口")
    @PutMapping
    public Result update(@RequestBody User user) {
        return userService.update(user);
    }


    @ApiOperation("统计总用户数接口")
    @GetMapping("/total")
    public Integer userTotal() {
        return userService.getUserTotal();
    }
}
